# 1. 导包
import unittest
import requests
from parameterized import parameterized
from read_json_data import build_data


class LoginApi:
    def __init__(self):
        # 实例属性，登陆网址
        self.login_url = "http://ihrm-test.itheima.net/api/sys/login"

    def login(self, body):
        """
        功能：发送登陆请求
        :param body: 发送请求的请求体
        :return: 发送登陆请求后的响应对象
        """
        resp = requests.post(self.login_url, json=body)
        return resp


class TestLogin(unittest.TestCase):
    # 1. build_data('login.json') 调用函数，获取返回值
    # 2. 返回值作为参数化的参数
    @parameterized.expand(build_data('login.json'))
    def test_login(self, desc, body, expect_result):
        print(f'desc = {desc}')
        print(f'body = {body}')
        print(f'expect_result = {expect_result}')

        # 发送请求
        # 实例化对象
        login_api = LoginApi()
        # 实例对象调用实例方法，获取响应对象
        response = login_api.login(body)
        print("json data =", response.json())

        # 断言
        json_data = response.json()
        self.assertEqual(200, response.status_code)
        self.assertEqual(expect_result.get('success'), json_data.get("success"))
        self.assertEqual(expect_result.get('code'), json_data.get("code"))
        self.assertIn(expect_result.get('message'), json_data.get("message"))
